package QC.MessageBox.beans.wxmsg.passivemsg;

import QC.MessageBox.beans.wxmsg.BaseWXMsg;
import io.vertx.core.json.JsonObject;

import java.util.List;

/**
 * Created by Andaren on 2017/5/13.
 * Used for: 被动回复图文消息
 */
public class WXArticleReply extends BaseWXMsg {

    private Integer articleCount;               // 文章个数

    private List<ArticleItem> itemList;         // 单个文章列表

    private static String BaseXml = "<xml>" +
            "<ToUserName><![CDATA[%1$s]]></ToUserName>" +
            "<FromUserName><![CDATA[%2$s]]></FromUserName>" +
            "<CreateTime>%3$s</CreateTime>" +
            "<MsgType><![CDATA[news]]></MsgType>" +
            "<ArticleCount>%4$s</ArticleCount>" +
            "<Articles>%5$s</Articles>" +
            "</xml>";

    private static String itemXml = "<item>" +
            "<Title><![CDATA[%1$s]]></Title>" +
            "<Description><![CDATA[%2$s]]></Description>" +
            "<PicUrl><![CDATA[%3$s]]></PicUrl>" +
            "<Url><![CDATA[%4$s]]></Url>" +
            "</item>";

    public String buildXmlReply() {
        // 拼接item
        StringBuilder ItemSb = new StringBuilder();
        for (int i = 0; i < articleCount; i++) {
            ArticleItem itemIndex = itemList.get(i);
            String itemFormat = String.format(itemXml,
                    itemIndex.getTitle(),
                    itemIndex.getDescription(),
                    itemIndex.getPicurl(),
                    itemIndex.getUrl());
            ItemSb.append(itemFormat);
        }
        // 拼接文章
        String newsStr = String.format(BaseXml,
                getToUserName(),
                getFromUserName(),
                getCreateTime(),
                getArticleCount(),
                ItemSb.toString());
        System.out.println("发送图文回复：" + newsStr);
        return newsStr;
    }

    public Integer getArticleCount() {
        return articleCount;
    }

    public WXArticleReply setArticleCount(Integer articleCount) {
        this.articleCount = articleCount;
        return this;
    }

    public List<ArticleItem> getItemList() {
        return itemList;
    }

    public WXArticleReply setItemList(List<ArticleItem> itemList) {
        this.itemList = itemList;
        return this;
    }

    /**
     * 主动发送图文信息
     *
     * {
         "touser":"OPENID",
         "msgtype":"news",
         "news":{
             "articles": [
                 {
                 "title":"Happy Day",
                 "description":"Is Really A Happy Day",
                 "url":"URL",
                 "picurl":"PIC_URL"
                 },
                 {
                 "title":"Happy Day",
                 "description":"Is Really A Happy Day",
                 "url":"URL",
                 "picurl":"PIC_URL"
                 }
             ]
         }
     }
     * @return
     */
    public static JsonObject buildJsonReply(String toUser, JsonObject articles) {
        JsonObject returnJson = new JsonObject();

        returnJson.put("touser", toUser)
                .put("msgtype", "news")
                .put("news", articles);

        return returnJson;
    }

    /**
     *  图文消息中的单个文章实体
     */
    public class ArticleItem {
        private String title;
        private String description;
        private String picurl;
        private String url;

        public String getTitle() {
            return title;
        }

        public ArticleItem setTitle(String title) {
            this.title = title;
            return this;
        }

        public String getDescription() {
            return description;
        }

        public ArticleItem setDescription(String description) {
            this.description = description;
            return this;
        }

        public String getPicurl() {
            return picurl;
        }

        public ArticleItem setPicurl(String picurl) {
            this.picurl = picurl;
            return this;
        }

        public String getUrl() {
            return url;
        }

        public ArticleItem setUrl(String url) {
            this.url = url;
            return this;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("ArticleItem{");
            sb.append("title='").append(title).append('\'');
            sb.append(", description='").append(description).append('\'');
            sb.append(", picurl='").append(picurl).append('\'');
            sb.append(", url='").append(url).append('\'');
            sb.append('}');
            return sb.toString();
        }
    }


}
